/*
 * Copyright 2010 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.kommando.web.google.translate.actions;

import org.kommando.core.action.ActionExecutionException;
import org.kommando.core.catalog.CatalogObject;
import org.kommando.core.catalog.TextObject;
import org.kommando.web.google.translate.actions.support.BaseTranslateAction;

import com.google.api.translate.Language;
import com.google.api.translate.Translate;

/**
 * Translates text.
 * 
 * @author Peter De Bruycker
 */
public class TranslateToAction extends BaseTranslateAction {

    private static final String ID = "web:google:translateTo";

    @Override
    public Class<?>[] getDirectExtensionTypes() {
        return new Class<?>[] { String.class };
    }

    @Override
    protected CatalogObject doExecute(CatalogObject direct, Language from, Language to) throws ActionExecutionException {
        String text = direct.getExtension(String.class);

        try {
            Translate.setHttpReferrer("localhost");
            String translated = Translate.execute(text, from, to);

            return new TextObject(translated);
        } catch (Exception e) {
            throw new ActionExecutionException(e);
        }
    }

    @Override
    public String getDescription() {
        return "Translate text";
    }

    @Override
    public String getName() {
        return "Translate to...";
    }

    @Override
    public String getId() {
        return ID;
    }

}
